﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Net;
using System.Xml;
using System.Text;
using System.IO;
namespace LHWebApp.Json
{
    /// <summary>
    /// JsonGetWeightInfoList 的摘要说明
    /// </summary>
    public class JsonGetWeightInfoList : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string op = context.Request.Params["op"];
            switch (op)
            {
                //case "getStore":
                //    GetAlarmList(context);
                //    break;
                case "GetVehicleWeightByFactoryAndDistrictBh":
                    getVehicleWeightByFactoryAndDistrictBh(context);
                    break;
                case "GetDistrictWeightAllBh":
                    getDistrictWeightAllBh(context);
                    break;
                case "GetFactoryWeightAllBh":
                    getFactoryWeightAllBh(context);
                    break;
                default:
                    context.Response.Write("{success: noFindOP}");
                    break;
            }
        }
        private void getVehicleWeightByFactoryAndDistrictBh(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string clientName = context.Request.Params["clientName"];
            string date = context.Request.Params["dateString"];
            string name = context.Request.Params["name"];
            try
            {
                DataSet ds = LHWebApp.Services.GetVehicleWeightByFactoryAndDistrictBh(clientName, date, name);
                DataTable dt = ds.Tables[2];
                string dateResponse = ds.Tables[0].Rows[0][0].ToString();
                string weightResponse = ds.Tables[1].Rows[0][0].ToString();
                JSONHelper jsonHelp = new JSONHelper();
                jsonHelp.success = true;
                jsonHelp.totlalCount = dt.Rows.Count;
                if (jsonHelp.totlalCount == 0)
                {
                    context.Response.Write("{success: true}");
                }
                else
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        jsonHelp.AddItem("name", dr[0].ToString());
                        jsonHelp.AddItem("dunCount", dr[1].ToString());
                        jsonHelp.AddItem("dateAll", dateResponse);
                        jsonHelp.AddItem("weightAll", weightResponse);
                        jsonHelp.ItemOk();
                    }
                    //jsonHelp.AddItem("dateString", ds.Tables[0].ToString());
                    //jsonHelp.AddItem("dunCountALL", ds.Tables[1].ToString());
                    context.Response.Write(jsonHelp.ToString());
                }
            }
            catch (Exception)
            {
                context.Response.Write("{success: false}");
            }
        }
        private void getFactoryWeightAllBh(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string date = context.Request.Params["dateString"];
            string name = context.Request.Params["name"];
            try
            {
                DataSet ds = LHWebApp.Services.GetFactoryWeightAllBh(date, name);
                DataTable dt = ds.Tables[2];
                string dateResponse = ds.Tables[0].Rows[0][0].ToString();
                string weightResponse = ds.Tables[1].Rows[0][0].ToString();
                JSONHelper jsonHelp = new JSONHelper();
                jsonHelp.success = true;
                jsonHelp.totlalCount = dt.Rows.Count;
                if (jsonHelp.totlalCount == 0)
                {
                    context.Response.Write("{success: true}");
                }
                else
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        jsonHelp.AddItem("name", dr[0].ToString());
                        jsonHelp.AddItem("type", dr[0].ToString());
                        jsonHelp.AddItem("dunCount", dr[1].ToString());
                        jsonHelp.AddItem("dateAll", dateResponse);
                        jsonHelp.AddItem("weightAll", weightResponse);
                        jsonHelp.ItemOk();
                    }
                    //jsonHelp.AddItem("dateString", ds.Tables[0].ToString());
                    //jsonHelp.AddItem("dunCountALL", ds.Tables[1].ToString());
                    context.Response.Write(jsonHelp.ToString());
                }
            }
            catch (Exception)
            {
                context.Response.Write("{success: false}");
            }
        }
        private void getDistrictWeightAllBh(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string date = context.Request.Params["dateString"];
            string typeLJ = context.Request.Params["typeLJ"];
            string type = context.Request.Params["type"];
            try
            {
                DataSet ds = LHWebApp.Services.GetDistrictWeightAllBh(date, type, typeLJ);
                DataTable dt = ds.Tables[2];
                string dateResponse = ds.Tables[0].Rows[0][0].ToString();
                string weightResponse = ds.Tables[1].Rows[0][0].ToString();
                JSONHelper jsonHelp = new JSONHelper();
                jsonHelp.success = true;
                jsonHelp.totlalCount = dt.Rows.Count;
                if (jsonHelp.totlalCount == 0)
                {
                    context.Response.Write("{success: true}");
                }
                else
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        jsonHelp.AddItem("name", dr[0].ToString());
                        jsonHelp.AddItem("dunCount", dr[1].ToString());
                        jsonHelp.AddItem("dateAll", dateResponse);
                        jsonHelp.AddItem("weightAll", weightResponse);
                        jsonHelp.ItemOk();
                    }
                    //jsonHelp.AddItem("dateString", ds.Tables[0].ToString());
                    //jsonHelp.AddItem("dunCountALL", ds.Tables[1].ToString());
                    context.Response.Write(jsonHelp.ToString());
                }
            }
            catch (Exception)
            {
                context.Response.Write("{success: false}");
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}